<?php
include('class.receiver.php');

if (isset($_POST['PRSVRC'])) {

	$qr_OBJ = $_POST['RID'];

	$qr_OBJCL = $_POST['CID'];
	$CLST = $_POST['CST'];

	$ACFi1 = $_POST['BnfNumExt'];
	if ($_POST['BnfNombre'] == '') {
		$Msg_ERROR = 'RC002';
	} else {
		$ACFi2 = req_form($_POST['BnfNombre']);
	}
	if ($_POST['BnfCedula'] == '' && isset($_POST['BnfDocType'])) {
		$Msg_ERROR = 'RC004';
	} elseif (!isset($_POST['BnfDocType'])) {
		$ACFi4 = 0;
		$_POST['BnfDocType'] = 0;
	} else {
		$ACFi4 = preg_replace("#[\-\/\.\ \,]#", "", req_form($_POST['BnfCedula']));
	}
	$ACFi5 = req_form($_POST['BnfDireccion']);
	$ACFi6 = req_form($_POST['BnfBarrio']);
	if (!isset($_POST['BnfCiudad'])) {
		$Msg_ERROR = 'RC007';
	} else {
		$ACFi7 = $_POST['BnfCiudad'];
	}
	if (!isset($_POST['BnfProvincia'])) {
		$ACFi8 = 'Null';
	} else {
		$ACFi8 = $_POST['BnfProvincia'];
	}
	$ACFi9 = $_POST['BnfPais'];
	$ACFi10 = req_form($_POST['BnfTelefono']);
	$ACFi11 = req_form($_POST['BnfTelefono2']);
	$ACFi17 = $sysTime;
	$ACFi19 = $_SESSION[$SESSID];
	$ACFi20 = $_POST['BnfDocType'];
	$ACFi21 = req_form($_POST['BnfNotes']);

	if ($_POST['BnfDocType'] == 8 && $_POST['BnfCedula'] == '04124922000161') {
		$Msg_ERROR = 'RC009';
	}

	if (!isset($Msg_ERROR)) {

		if ($qr_OBJ == 0) {
			$sql = "INSERT
			INTO officeschema.beneficiarios
			(BnfNumExt,
				BnfNombre,
				BnfCedula,
				BnfDireccion,
				BnfBarrio,
				BnfCiudad,
				BnfProvincia,
				BnfPais,
				BnfTelefono,
				BnfTelefono2,
				BnfFechaAlta,
				BnfUsuario,
				BnfDocType,
				BnfNotes)
			VALUES
			({$ACFi1},'{$ACFi2}','{$ACFi4}','{$ACFi5}','{$ACFi6}',{$ACFi7},
			{$ACFi8},{$ACFi9},'{$ACFi10}','{$ACFi11}','{$ACFi17}',{$ACFi19},
			{$ACFi20},'{$ACFi21}')";

			$PRRC = $GLOBALS['MYT']->db_query($sql);

			//================= Associa Bnf a Cli =====================

			$sql = "SELECT
			BnfNumero FROM officeschema.beneficiarios WHERE BnfUsuario = {$_SESSION[$SESSID]} AND BnfFechaAlta = '{$sysTime}'
			ORDER BY BnfNumero DESC
			LIMIT 0,1";
			$SLRC = $GLOBALS['MYT']->db_array($sql);
			$qr_OBJRC = $SLRC[1]['BnfNumero'];

			$PRRCCL = $GLOBALS['MYT']->db_query("INSERT
			INTO unionclientesbeneficiarios
			(uniCliente, uniBeneficiario)
			VALUES
			({$qr_OBJCL},{$qr_OBJRC})");
			$qr_OBJ = $qr_OBJRC;
		} elseif ($qr_OBJ > 0) {
			include($GLOBALS['BIB'] . 'Pr_db/pr_historics.mex');
			hs_receiver($qr_OBJ, $sysTime);
			$GLOBALS['MYT']->db_query("UPDATE
			Beneficiarios
			SET 
				BnfNumExt = {$ACFi1}, 
				BnfNombre = '{$ACFi2}', 
				BnfCedula = '{$ACFi4}', 
				BnfDireccion = '{$ACFi5}', 
				BnfBarrio = '{$ACFi6}', 
				BnfCiudad = {$ACFi7}, 
				BnfProvincia = {$ACFi8}, 
				BnfPais = {$ACFi9}, 
				BnfTelefono = '{$ACFi10}', 
				BnfTelefono2 = '{$ACFi11}', 
				BnfFechaAlta = '{$ACFi17}', 
				BnfUsuario = {$ACFi19},
				BnfDocType = {$ACFi20},
				BnfNotes = '{$ACFi21}'
			WHERE BnfNumero = {$qr_OBJ}");
		}
		if ($_POST['uniRelation'] > 0) {
			$GLOBALS['MYT']->db_query("UPDATE unionclientesbeneficiarios
			SET uniRelation = {$_POST['uniRelation']} 
			WHERE uniCliente = {$qr_OBJCL} AND uniBeneficiario = {$qr_OBJ}");
		}
		echo '<script charset="utf-8" type="text/javascript">replaceUrl(\'ClientProp\',\'_response/call.php?get=Operative/receiver&CID=' . $qr_OBJCL . '&CST=' . $CLST . '\');</script>';
	} else {
	 echo '<script charset="utf-8" type="text/javascript">
            document.getElementById(\'RCMSG\').innerHTML=\'<div class="Error">'.$error_msg[$Msg_ERROR].'</div>\';</script>';
   }
}
